from django.shortcuts import render, HttpResponse
from django import views
from app01 import models
import json

class BaseResponse(object):
	def __init__(self):
		self.status = True
		self.data = None
		self.message = None

class ServerView(views.View):
	def get(self, request, *args, **kwargs):
		return render(request, 'server.html')

class ServerJsonView(views.View):
	def get(self, request, *args, **kwargs):
		response = BaseResponse()
		try:
			table_config = [
				{
					'q': 'hostname',
					'title':'主机名',
					'display':1,
				},
				{
					'q': 'port',
					'title':'端口',
					'display':1,
				},
				{
					'q': 'business_unit',
					'title': '业务线ID',
					'display': 1,
				},
				{
					'q': None,
					'title': '操作',
					'display': 1,
				}
			]
			values_list = []
			for item in table_config:
				if item['q']:
					values_list.append(item['q'])

			data_list = models.Server.objects.values(*values_list)
			print(data_list)
			data_list = list(data_list)  # 将queryset 转为list, 方便序列化。

			response.data = {
				'table_config': table_config,
				'data_list': data_list,
			}
		except Exception as e:
			response.status = False
			response.message = str(e)
		return HttpResponse(json.dumps(response.__dict__))

class BusinessView(views.View):
	def get(self, request, *args, **kwargs):
		return render(request, 'business.html')

class BusinessJsonView(views.View):
	def get(self, request, *args, **kwargs):
		response = BaseResponse()
		try:
			table_config = [
				{
					'q': 'id',
					'title':'ID',
					'display':1,
				},
				{
					'q': 'name',
					'title':'业务线名称',
					'display':1,
				},
				{
					'q': None,
					'title': '操作',
					'display': 1,
				}
			]
			values_list = []
			for item in table_config:
				if item['q']:
					values_list.append(item['q'])

			data_list = models.BusinessUnit.objects.values(*values_list)
			print(data_list)
			data_list = list(data_list)  # 将queryset 转为list, 方便序列化。

			response.data = {
				'table_config': table_config,
				'data_list': data_list,
			}
		except Exception as e:
			response.status = False
			response.message = str(e)
		return HttpResponse(json.dumps(response.__dict__))